package com.zzh.htts.voice;


/**
 * Created by zzh on 2024/12/23.
 *
 * @date: 2024/12/23 11:37
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.htts.voice
 */

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BluetoothHeadsetListener {

    private Context mContext;
    private BroadcastReceiver mBluetoothReceiver;

    public BluetoothHeadsetListener(Context context) {
        this.mContext = context;
        mBluetoothReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // 蓝牙耳机已连接
                    handleHeadsetConnected(device);
                } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // 蓝牙耳机已断开
                    handleHeadsetDisconnected(device);
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        mContext.registerReceiver(mBluetoothReceiver, filter);
    }

    private void handleHeadsetConnected(BluetoothDevice device) {
        // 处理蓝牙耳机连接逻辑
    }

    private void handleHeadsetDisconnected(BluetoothDevice device) {
        // 处理蓝牙耳机断开逻辑
    }

    public void unregister() {
        if (mBluetoothReceiver != null) {
            mContext.unregisterReceiver(mBluetoothReceiver);
        }
    }
}
